<template>
	<view>
		<image mode="widthFix" class="logoImg" src="../../static/images/logo.jpg" alt="" srcset="" />
		<view class="form" v-if="!start">
			<!-- <input type="number" v-model="num" placeholder="请输入抽奖人数" /> -->

			<view class="btn" @click="choujiang" style="margin-top: 40rpx;" v-if="choujiangConfig.inTime">
				抽奖
			</view>
			
		</view>

		<view class="form" v-else>
			<view class="overNum">
				<span style="font-size: 32rpx;">积分：</span> {{getNum}}
			</view>

			<view class="btn" @click="overChoujiang">
				结束抽奖
			</view>
		</view>

		<view class="time">
			系统时间：{{formattedTime}}
		</view>


		<!-- <view class="delArrbox" v-if="delArr.length>0 && start">
			<view class="title" style="text-align: center;">
				抽中号码
			</view>
			<view class="checkbox">
				<span class="tag" v-for="(item,index) in delArr" :key="index">{{item}}</span>
			</view>
		</view> -->
	</view>
</template>

<script>
	import {getConfig,integralLotterysave} from '@/api/api.js';
	export default {
		components: {},
		data() {
			return {
				num: '',
				formattedTime: '',
				timeer: null,
				startTimeer: null,
				start: false,
				getNum: 0,
				xunhuanarr: [],
				delArr: [],
				canchoujiang:false,
				choujiangConfig:{}
			};
		},
		onShow() {},
		onLoad() {
			this.getConfig()
			this.timeer = setInterval(() => {
				this.getTime()
			}, 1000)
		},
		onHide() {
			clearInterval(this.timeer)
			this.timeer = null

			if (this.startTimeer) {
				clearInterval(this.startTimeer)
				this.startTimeer = null
			}
		},
		onUnload() {
			clearInterval(this.timeer)
			this.timeer = null

			if (this.startTimeer) {
				clearInterval(this.startTimeer)
				this.startTimeer = null
			}
		},
		methods: {
			getConfig(){
				getConfig().then(res=>{
					console.log('抽奖配置')
					console.log(res.data)
					
					
					this.choujiangConfig = res.data
					
				})
			},
			getTime() {
				const now = new Date();

				const year = now.getFullYear();
				const month = ('0' + (now.getMonth() + 1)).slice(-2);
				const day = ('0' + now.getDate()).slice(-2);
				const hours = ('0' + now.getHours()).slice(-2);
				const minutes = ('0' + now.getMinutes()).slice(-2);
				const seconds = ('0' + now.getSeconds()).slice(-2);

				this.formattedTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
			},
			choujiang() {
				this.startGame()
			},
			
		
			startGame() {
				this.start = true

				this.startTimeer = setInterval(() => {
					this.getNum = this.getRandomArbitrary(parseFloat(this.choujiangConfig.min), parseFloat(this.choujiangConfig.max))
				}, 100)
			},
			getRandomArbitrary(min, max) {
				return (Math.random() * (max - min) + min).toFixed(1);
			},
			overChoujiang() {
				if (this.startTimeer) {
					clearInterval(this.startTimeer)
					this.startTimeer = null
				}
				
				uni.showLoading({
					title:'加载中...',
					mask:true
				})
				let data = {
					point:this.getNum,
				}
				
				integralLotterysave(data).then(res=>{
					uni.hideLoading()
					this.$util.Tips({
						title: `抽奖结束，积分 +${this.getNum}`
					});
					
					this.start = false
				})
				
			},
			back() {
				this.overChoujiang()
				this.start = false
			},


		}
	};
</script>

<style>
	page {
		background-color: #fff !important;
	}
</style>
<style lang="scss" scoped>
	.logoImg {
		width: 200rpx;
		height: 200rpx;
		margin: 8% auto;
		display: block;
	}
.scroll_box {
		background: #FFFFFF;
		margin: 30rpx;
		border-radius: 10rpx;

		.swiper {
			height:250px;
		}
	}
	.otherText {
		text-align: center;
		margin-top: 50rpx;
		font-size: 30rpx;
	}

	.form {
		width: 650rpx;
		margin: 0 auto;

		.overNum {
			text-align: center;
			font-size: 120rpx;

		}

		input {
			width: 100%;
			height: 80rpx;
			border: 1px solid #d4d4d4;
			border-radius: 10rpx;
			box-sizing: border-box;
			padding: 0 30rpx;
			font-size: 30rpx;
		}



		.btn {
			width: 350rpx;
			height: 90rpx;
			background-color: #e02021;
			border-radius: 100px;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			margin: 0 auto;
			margin-top: 100rpx;
		}
	}

	.time {
		text-align: center;
		font-size: 28rpx;
		margin-top: 40rpx;
	}

	.delArrbox {
		width: 600rpx;
		margin: 0 auto;
		margin-top: 50rpx;

		.checkbox {
			width: 600rpx;
			margin: 0 auto;
			padding-left: 20rpx;
		}

		.tag {
			width: 80rpx;
			height: 80rpx;
			background-color: #ff0000;
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			font-size: 34rpx;
			display: inline-block;
			margin-right: 40rpx;
			margin-top: 20rpx;
			line-height: 80rpx;
			text-align: center;
		}
	}
</style>